import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/index/index.vue'),
    meta: { requiresAuth: true },
    redirect: '/curriculum',
    children: [
      {
        path:'/curriculum',
        name: 'curriculum',
        component: () => import('../views/curriculum/index.vue'),
        meta: { requiresAuth: true },
      },
      {
        path:'/details',
        name: 'curriculum',
        component: () => import('../views/curriculum/details.vue'),
        meta: { requiresAuth: true },
      },
      {
        path:'/user',
        name: 'user',
        component: () => import('../views/curriculum/user.vue'),
        meta: { requiresAuth: true },
      },{
        path:'/list',
        name: 'user',
        component: () => import('../views/list/index.vue'),
        meta: { requiresAuth: true },
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 路由前置守卫
router.beforeEach((to, from, next) => {
  const userInfo = JSON.parse(localStorage.getItem('userInfo'))
  if (userInfo) {
    // console.log('to',to)
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')

    }
  }
});
export default router
